<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/16
 * Time: 13:30
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Member extends Base {
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map['user_status'] = ['<>', 2];
            extract(input());
            if(isset($phone) && $phone != ''){
                $map['user_phone'] = ['like', '%' . $phone . '%'];
            }
            if(isset($sex)&&$sex!="")
            {
                $map['user_gender'] = $sex;
            }
            if(isset($user_role)&&$user_role!="")
            {
                $map['user_role'] = $user_role;
            }
            if(isset($province)&&$province!="")
            {
                $map['user_sheng'] = $province;
            }

            if(isset($city)&&$city!="")
            {
                $map['user_shi'] = $city;
            }
            if(isset($district)&&$district!="")
            {
                $map['user_qu'] = $district;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['user_time'] = ['between time',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($map)->count();// 获取总条数
            $count2 = Db::name('u_user')->where($map)->column('user_id');// 获取总条数
            $lists = Db::name('u_user')
                ->join('up_area ua1', 'ua1.district_id = user_sheng', 'LEFT')
                ->join('up_area ua2', 'ua2.district_id = user_shi', 'LEFT')
                ->join('up_area ua3', 'ua3.district_id = user_qu', 'LEFT')
                ->where($map)
                ->field('user_id, user_username, user_car_id, user_phone, user_time, user_role, 
                user_is_allow_look, user_status, ua1.district sheng, ua2.district shi, ua3.district qu')
                ->page($NowPage,$limits)
                ->order('user_time desc')
                ->select();
            foreach ($lists as $key => $vo){
                $lists[$key]['address'] = $vo['sheng'].$vo['shi'].$vo['qu'];
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     *  解冻
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *  冻结
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_status',3);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     *  设置经销商权限
     */
    public function up1(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_role',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *  取消经销商权限
     */
    public function down1(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_role',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    
    /***
     * @return \think\response\Json
     *  设置车辆状态权限
     */
    public function up2(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_is_allow_look',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    /***
     * @return \think\response\Json
     *  取消车辆状态权限
     */
    public function down2(){
        $task_id = input('param.id');
        $res = Db::name('u_user')->where('user_id',$task_id)->setField('user_is_allow_look',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        $user_ids = input('ids');
        if($user_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的用户']);
        }
        $lists = Db::name('u_user')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'LEFT')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'LEFT')
            ->join('up_area ua3', 'ua3.district_id = user_qu', 'LEFT')
            ->field('user_id, user_username, user_phone, user_time, user_status, user_role, ua1.district sheng, 
            ua2.district shi, ua3.district qu')
            ->where('user_id', 'IN', $user_ids)
//            ->order('user_time desc')
            ->select();
        foreach ($lists as $key => $vo){
            $lists[$key]['address'] = $vo['sheng'] . $vo['shi'] . $vo['qu'];
            if($vo['user_role']==1){
                $lists[$key]['user_role_name'] = '是';
            }else{
                $lists[$key]['user_role_name'] = '否';
            }
            if($vo['user_status']==1){
                $lists[$key]['user_status_name'] = '正常';
            }else if($vo['user_status']==3){
                $lists[$key]['user_status_name'] = '冻结';
            }
            $lists[$key]['user_phone'] = "\t".$vo['user_phone']."\t";
        }
        $cellname = [
            ['user_id','用户ID',10,'LEFT'],
            ['user_username','用户名',20,'LEFT'],
            ['user_phone','手机号',20,'LEFT'],
            ['user_status_name','用户状态',20,'LEFT'],
            ['user_role_name','是否有经销商权限',20,'LEFT'],
            ['address','地区',40,'LEFT'],
            ['user_time','注册时间',20,'LEFT'],
        ];
        $res = exportExcel('用户列表','Users',$cellname,$lists);
        return json($res);
    }
    
    /**
     * 编辑车联网账号
     * @return mixed|\think\response\Json
     */
    public function editCarID(){
        $user_id = input('user_id');
        if(Request::instance()->isPost()){
            $carID = input('carID');
            $car_url = 'http://cctruck.project.g7s.huoyunren.com/service.php?method=truck.ccappapi.getVehicleTeam';
            $request = $car_url . '&account=' . $carID;
            $data = kd_http($request);
            if($data['procStatus'] == 0){
                $team = end($data['teams']);
                $num = count($data['teams']);
                $res = Db::table('up_u_user')->where('user_id', $user_id)->update([
                    'user_update_time' => date('Y-m-d H:i:s'),
                    'user_car_id' => $carID,
                    'user_team_id' => $team['teamId'],
                    'user_car_num' => $num
                ]);
                if($res){
                    return json(['code' => 200, 'msg' => '编辑成功']);
                }else{
                    return json(['code' => 100, 'msg' => '编辑失败']);
                }
            }else{
                return json(['code' => 100, 'msg' => $data['message']]);
            }
        }
        $carID = Db::table('up_u_user')->where('user_id', $user_id)->value('user_car_id');
        $this->assign('carID', $carID);
        $this->assign('user_id', $user_id);
        return $this->fetch('member/editcarid');
    }
}